package com.databias.j15;

import java.awt.Graphics2D;
import java.util.LinkedList;
import java.util.Queue;

/**
 * This class is a scrolling history graph.  It contains an array of values which are shifted
 * as new values come in, using a queue.
 * 
 * This current version uses a series of GBars; simplify the class to use line segments.
 * TODO: make a set of points rather than bars.
 * 
 * @author Frederick
 *
 */
public class GHistogram implements GObject{
	
	private int numDatapoints=10;
	private int graphHeight=10;
	private final int MAXSIZE=100;
	private Queue<Integer> myQueue;
	
	GBar[] bars;
	
	// The logical choice for a scrolling graph is to have an array of values
	// but the queue class doesn't seem to have a good traversal method
	
	/**
	 * Create a histogram at the location specified.  This coupling is because 
	 * the <code>gbar object needs an internal representation (deprecated) 
	 */
	public GHistogram (int x, int y, int numberOfsegments, int height)
	{
		if (numberOfsegments>numDatapoints)
			numDatapoints=numberOfsegments;
		
		if (height>graphHeight)
			graphHeight=height;
		
		if (numDatapoints>MAXSIZE)
			numDatapoints=MAXSIZE;

		myQueue = new LinkedList<Integer>();
		
		// Fill the queue with empty values;
		// TODO: isn't there an array.fill option?
		
		for (int i=0; i< numDatapoints; i++)
			myQueue.offer(0);

		// Set up an array of bars
		bars =new GBar[numDatapoints];
		
		// Create a series of single-pixel bars
		for (int i=0; i<numDatapoints; i++)
		{
			GBar b=new GBar(x+i,y, 1, graphHeight,GBar.VERTICAL, GBar.NOOUTLINE);
			b.setMaxValue(100); // percentage

			//Set max and min
			bars[i]=b;
		}
		
	}
	
	/**
	 * Add a new element to the queue
	 * @param i
	 */
	public void add(Integer i)
	{
		//Pull the head off
		myQueue.poll();
		//Add the tail
		myQueue.add(i);
	}
	
	private void  dumpit()
	{
		for (Integer o : myQueue)
		    System.out.println(o);
	}

	public void draw(int x, int y, Graphics2D gs) {
		
		int n=0;
		
		for (Integer qv: myQueue)
		{
			bars[n].setValue(qv.intValue());
			bars[n].draw(0, 0, gs);
			n++;
		}

	}
	    
}
